package com.wlyuan.open.scanner.application.handler.table;

import com.alibaba.fastjson.JSON;
import com.wlyuan.open.scanner.application.annotation.TableScannerName;
import com.wlyuan.open.scanner.constant.ScannerCodeEnum;
import com.wlyuan.open.scanner.constant.TableEnum;
import com.wlyuan.open.scanner.utils.enums.tenant.ShareCompanyTypeEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author lijing
 * @description
 */
@Slf4j
@Service
@RequiredArgsConstructor
@TableScannerName(isDefault = false, tableName = TableEnum.TENANT)
public class TenantTableToIncrHandler extends BaseTableToIncrHandler {
    private static final String COMPANY_PERSONAL = "Company_Personal";

    @Override
    public List<String> handler(String tableName, String data, String beforeData) {

        var businessParams = params(data);

        if (StringUtils.isBlank(businessParams)) {
            logger.error("{}表业务分流参数不能为空", tableName);
            return Collections.emptyList();
        }
        var type = Integer.valueOf(businessParams);
        //企业
        if (ShareCompanyTypeEnum.isCompany(type)) {
            return Arrays.asList(ScannerCodeEnum.COMPANY_TENANT.getName());
        }
        //个体司机租户
        if (ShareCompanyTypeEnum.isPersonal(type)) {
            return Arrays.asList(ScannerCodeEnum.DRIVER_TENANT.getName());
        }
        return Collections.emptyList();
    }

    private String params(String data) {
        var map = JSON.parseObject(data, Map.class);
        //businessParams 为 Company_Personal
        var businessParams = String.valueOf(map.get(COMPANY_PERSONAL));
        logger.info("company Company_Personal params : {}", businessParams);
        return businessParams;
    }
}
